package com.iphanta.community.activity.voting.influxdb;

import com.influxdb.client.*;
import com.influxdb.client.domain.WritePrecision;
import com.iphanta.community.activity.voting.entity.Vote;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/8/22 3:06 PM
 */
public class InfluxdbUtils {

    private static String url = "http://192.168.64.3:8086/";

    private static String org = "huangzehong";

    public static String bucket = "vote_activity";

    private static InfluxDBClient client;

    static {
        InfluxDBClientOptions options = InfluxDBClientOptions.builder()
                .url(url)
                .org(org)
                .bucket(bucket)
                .authenticateToken("JoT1auZrRCRPdWcXz66JdeOf5JCXxVELEk4VxbMDOpSgZ5cfDymwNnRPlyOli6RZDmuiAsJ46YxhLLUHQX0IGQ==".toCharArray())
//                .okHttpClient(builder)
                .build();
        client = InfluxDBClientFactory.create(options);

    }

    public static <M> void insert(M measurement) {
        WriteApiBlocking writeApi = client.getWriteApiBlocking();
        writeApi.writeMeasurement(WritePrecision.NS, measurement);
    }

    public static <M> List<M> query(String flux, Class<M> mClass) {
        System.out.println(flux);
        QueryApi queryApi = client.getQueryApi();
        return queryApi.query(flux, mClass);
    }

}
